<?php
class Publication_model extends General_db_model
{
	private $tbl_publication = 'tbl_publication_info';
	private $tbl_quantity = 'tbl_publication_quantity';
	
	function get_last_code($cat_id, $sub_cat_id)
	{
		$last_code_r = $this->getLast($this->tbl_publication, 'coding', 'id', 'category_id = '.$cat_id.' and sub_category_id = '.$sub_cat_id);
	
		if($last_code_r)
			return $last_code_r->coding;
		else
		{
			$cat_code_r = $this->getById('tbl_publication_category', 'id', $cat_id, 'code');
			$cat_code = $cat_code_r->code;
			$sub_cat_code_r = $this->getById('tbl_publication_category', 'id', $sub_cat_id, 'code');
			if($sub_cat_code_r)
				$sub_cat_code = $sub_cat_code_r->code;
			else
				$sub_cat_code = '';
			return $cat_code.$sub_cat_code;	
		}
	}
	
	function total_publications($where=NULL)
	{
		if($where)
			$this->db->where($where);
		$this->db->from($this->tbl_publication);
		return $this->db->count_all_results();
	}
	
	function get_publications($limit=NULL, $start=NULL, $where=NULL)
	{
		$this->db->select('a.*, b.category_name as thematic_area, c.category_name as sub_thematic_area, d.location_name');
		$this->db->from($this->tbl_publication.' as a');
		$this->db->order_by('a.id','DESC');
		$this->db->join('tbl_publication_category as b', 'a.category_id = b.id');
		$this->db->join('tbl_publication_category as c', 'a.sub_category_id = c.id', 'left');
		$this->db->join('tbl_locations as d', 'a.location = d.id', 'left');
		if($where)
			$this->db->where($where);
		$query = $this->db->get('', $limit, $start);
		return $query->result();
	}
	
	function get_quantity($publication_id, $type, $select='*')
	{
		$this->db->select($select);
		$this->db->where('publication_id', $publication_id);
		$this->db->where('type', $type);
		$query = $this->db->get($this->tbl_quantity);
		$result = $query->row();
		if($select == '*')
			return $result;
		else
		{
			return $result->$select;
		}
	}
	
	function get_publication_by_id($id, $select='*')
	{
	 	$result = $this->getById($this->tbl_publication, 'id', $id, $select);
		if($select == '*')
			return $result;
		else
			return $result->$select;
	}
	
	function get_publication_detail($id)
	{
		$this->db->select('a.*, b.category_name as thematic_area, c.category_name as sub_thematic_area');
		$this->db->from($this->tbl_publication.' as a');
		$this->db->join('tbl_publication_category as b', 'a.category_id = b.id');
		$this->db->join('tbl_publication_category as c', 'a.sub_category_id = c.id', 'left');
		$this->db->where('a.id', $id);
		$query = $this->db->get();
               // echo $this->db->last_query();
		return $query->row();
	}
	
	function get_publication_list()
	{
		$publications = $this->get_publications();
		foreach($publications as $publication){
			$publication_id = $publication->id;
			$publication_name = $publication->title;
			$data[$publication_id] = $publication_id." ($publication_name)";
		}
		return $data;
	}
	
	function get_publications_thematic_area()
	{
		$this->db->select('a.*');
		$this->db->from('tbl_publication_category as a');
		$this->db->join('tbl_publication_info as b', 'a.id = b.category_id');
		$this->db->group_by('b.category_id');
		$this->db->order_by('a.id','asc');
		$query = $this->db->get();
		return $query->result();
	}
         function check_title($keyword){
            $this->db->select('id, title');
            $where = $this->db->where('title',$keyword);
            $query = $this->db->get($this->tbl_publication);
            $result = $query->result_array();
            return $result;//debug_array($result);exit;
        }
        
        function get_thematic_area($id)
	{
		$this->db->select('*');
		$this->db->where('id', $id);
		$query = $this->db->get('tbl_publication_category');
		return $query->result();
	}
}
?>